var coordinateID = 'coordinate';
var deviceID = 'device';

$(function() {
	if(itemno == '1005'){
		$('.hidden_1005').hide();
	}
	//	增加坐标
	$('#coordinateAdd').click(function(){
		bindCoordinateList(null);
	});
	
	//	增加设备及技术参数
	$('#deviceAdd').click(function(){
		bindDeviceList(null);
	});

	//	生成地点范围
	$('#coordinateCreate').click(function(){
		var data = getCoordinateData();
		for(var i = 0; i<data.length; i++){
			var coordinate = data[i];

			var d = '经度'+ coordinate['lonarea'] +'，'+ coordinate['londegree'] +'°'+ coordinate['lonmin'] +'′'+ coordinate['lonsecond'] +'′′，纬'+ coordinate['latarea'] +'，'+ coordinate['latdegree'] +'°'+ coordinate['latmin'] +'′'+ coordinate['latsecond'] +'′′；';
			
			$('#workarea').val($('#workarea').val() + d);
		}
	});
	
	if(getUrlParam("applyno") == null){
		bindDict('worktypecode', 'DM_SSSXZYLX', null, null);
	//	bindDict('workpositioncode', 'DM_SSSXZYWZ', null, null);
		bindDict('workmethodcode', 'DM_SSSXZYFS', null, null);
	}
	
	bindWaterway('waterway', $('#orgcode').val(), null);
	$('#waterway').change(function(){
		var waterwaycode = $(this).val();
		
	
		bindMark('mark', waterwaycode, null);
	});
})

var index = 1;
function bindCoordinateList(data){
	$('#' + coordinateID).find('.emptyTr').remove();
    
	
	var htmlStr = ''+		
				'  <tbody class="coordinateForm_'+ index +' coordinateForm">'+
				'  <tr class="tr_'+ index +'">'+
				'    <td rowspan="2" style="vertical-align: middle;">'+
				'    	<input type="number" class="form-control serialnumber" style="text-align: center;" value="'+ index +'" name="serialnumber" no="N" />'+
				'	</td>'+
				'    <td>   '+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">经度</label>'+
				'		    <div class="col-sm-8">'+
				'			<select class="form-control" name="lonarea" no="N">'+
				'				<option value="E">E</option>'+
				'				<option value="W">W</option>'+
				'			</select>'+
				'		    </div>'+
				'		  </div> '+
				'    </td>'+
				'    <td><input type="number" class="form-control" placeholder="度" value="0" name="londegree" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="分" value="0" name="lonmin" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="秒" value="0" name="lonsecond" no="N"></td>'+
				'    <td rowspan="2" style="vertical-align: middle;">'+
				'		<button type="button" onclick="coordinateDelete('+ index +');" class="btn btn-danger btn-sm" style="margin-right: 10px;">'+
				'			<span class="glyphicon glyphicon-minus"></span>'+
				'		</button>'+
				'	  </td>'+
				'  </tr>'+
				'  <tr class="tr_'+ index +'">'+
				'    <td>'+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">纬度</label>'+
				'		    <div class="col-sm-8">'+
				'				<select class="form-control" name="latarea" no="N">'+
				'					<option value="N">N</option>'+
				'					<option value="S">S</option>'+
				'				</select>'+
				'		    </div>'+
				'		  </div> '+
				'	  </td>'+
				'    <td><input type="number" class="form-control" placeholder="度" value="0" name="latdegree" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="分" value="0" name="latmin" no="N"></td>'+
				'    <td><input type="number" class="form-control" placeholder="秒" value="0" name="latsecond" no="N"></td>'+
				'  </tr>'+
				'  </tbody>';
	
	$('#' + coordinateID).append(htmlStr);
	
	if(data != null){
		var serialnumber = data.serialnumber;
		var latarea = data.latarea;
		var latdegree = data.latdegree;
		var latmin = data.latmin;
		var latsecond = data.latsecond;
		var lonarea = data.lonarea;
		var londegree = data.londegree;
		var lonmin = data.lonmin;
		var lonsecond = data.lonsecond;
		
		$('.tr_' + index).find('[name="serialnumber"]').val(serialnumber);
		$('.tr_' + index).find('[name="latarea"]').val(latarea);
		$('.tr_' + index).find('[name="latdegree"]').val(latdegree);
		$('.tr_' + index).find('[name="latmin"]').val(latmin);
		$('.tr_' + index).find('[name="latsecond"]').val(latsecond);
		$('.tr_' + index).find('[name="lonarea"]').val(lonarea);
		$('.tr_' + index).find('[name="londegree"]').val(londegree);
		$('.tr_' + index).find('[name="lonmin"]').val(lonmin);
		$('.tr_' + index).find('[name="lonsecond"]').val(lonsecond);
	}
	if(index > 1){
		var rowsVal = $('#workarea').attr("rows");
		$('#workarea').attr("rows", Number.parseInt(rowsVal) + 6);
	}
	index++;
}


function bindCoordinateInfoList(data){
	var serialnumber = data.serialnumber;
	var latarea = data.latarea;
	var latdegree = data.latdegree;
	var latmin = data.latmin;
	var latsecond = data.latsecond;
	var lonarea = data.lonarea;
	var londegree = data.londegree;
	var lonmin = data.lonmin;
	var lonsecond = data.lonsecond;
	
	var htmlStr = ''+		
				'  <tbody class="coordinateForm">'+
				'  <tr>'+
				'    <td rowspan="2" style="vertical-align: middle;">'+
				'		'+ serialnumber +''+
				'	 </td>'+
				'    <td>   '+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">经度</label>'+
				'		    <div class="col-sm-8">'+
				'				'+ latarea +''+
				'		    </div>'+
				'		  </div> '+
				'    </td>'+
				'    <td>'+ latdegree +'</td>'+
				'    <td>'+ latmin +'</td>'+
				'    <td>'+ latsecond +'</td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td>'+
				'	      <div class="form-group">'+
				'		    <label class="col-sm-4 control-label">纬度</label>'+
				'		    <div class="col-sm-8">'+
				'				'+ lonarea +''+
				'		    </div>'+
				'		  </div> '+
				'	  </td>'+
				'    <td>'+ londegree +'</td>'+
				'    <td>'+ lonmin +'</td>'+
				'    <td>'+ lonsecond +'</td>'+
				'  </tr>'+
				'  </tbody>';
	
	$('#' + coordinateID).append(htmlStr);
	
}

function coordinateDelete(index){
	$('.coordinateForm_' + index).remove();
	if(index > 1){
		var rowsVal = $('#workarea').attr("rows");
		$('#workarea').attr("rows", Number.parseInt(rowsVal) - 6);
	}
	emptyTr();
}

var deviceIndex = 1;
function bindDeviceList(data){
	var htmlStr = ''+		
	'  <tbody class="deviceForm_'+ deviceIndex +' deviceForm">'+
	'  <tr class="tr_'+ deviceIndex +'">'+
	'    <td><input class="form-control" name="devicename" placeholder="请输入设备名称" no="N" /></td>'+
	'    <td><input class="form-control" name="summary" placeholder="请输入主要技术参数" no="N" /></td>'+
	'    <td style="vertical-align: middle;">'+
	'		<button type="button" onclick="deviceDelete('+ deviceIndex +');" class="btn btn-danger btn-sm" style="margin-right: 10px;">'+
	'			<span class="glyphicon glyphicon-minus"></span>'+
	'		</button>'+
	'	  </td>'+
	'  </tr>'+
	'  </tbody>';
	
	$('#' + deviceID).append(htmlStr);
	
	if(data != null){
		
		$('.tr_' + (deviceIndex)).find('[name="devicename"]').val(data.devicename);
		$('.tr_' + (deviceIndex)).find('[name="summary"]').val(data.summary);
	}
	
	deviceIndex++;
}

function bindDeviceInfoList(data){
	
	var htmlStr = '  <tbody class="deviceForm">'+
				  '  <tr>'+
				  '    <td>'+data.devicename+'</td>'+
				  '    <td>'+data.summary+'</td>'+
				  '  </tr>'+
				  '  </tbody>';	
	
	$('#' + deviceID).append(htmlStr);
}

function deviceDelete(index){
	$('.deviceForm_' + index).remove();
}

function bindShipList(data){
//	var str;
//	if(data.verifyresult == 1)
//		str = '<img src="../img/jiaoyan1.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="checkInfo('+JSON.stringify(data.verify).replace(/\"/g,"'")+')" title="点击查看详情">'; 
//	else if(data.verifyresult == 0)
//		str = '<img src="../img/jiaoyan2.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="checkInfo('+JSON.stringify(data.verify).replace(/\"/g,"'")+')" title="点击查看详情">'; 
//	else if(data.verifyresult == -1)
//		str = '<img src="../img/jiaoyan3.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="checkInfo('+JSON.stringify(data.verify).replace(/\"/g,"'")+')" title="点击查看详情">'; 
//	else   //正在校验中
//		str = '<img src="../img/loading.gif" alt="" style="width:20px;height:20px;" onclick="" title="数据校验中">';
	var htmlStr = ''+		
				'<tr id="tr_'+ data.shipidnumber +'">'+
				'	<td>'+ data.shipidnumber +'</td>'+
				'	<td>'+ data.shipname +'</td>'+
//				'	<td><input type="text" class="form-control" id="'+ data.shipidnumber +'" name="technicalparameter" /></td>'+
				'	<td>'+
				'		<input type="hidden" name="ship" id="ship'+data.shipidnumber+'" value=\''+ JSON.stringify(data) +'\'>'+
				'		<button onclick="deleteShip(\''+data.shipidnumber+'\');" type="button" class="btn btn-danger btn-sm" style="margin-right: 10px;">'+
				'			<span class="glyphicon glyphicon-minus"></span>'+
				'		</button>'+
				'	</td>'+
				'</tr>';

	$('#TableShip').append(htmlStr);
}


function deleteShip(shipidnumber){
	$('#tr_' + shipidnumber).remove();
}

function bindShipInfoList(data){
//	var str = "";
//	if(data.verifyresult >0)
//		str = '<img src="../img/jiaoyan1.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == 0)
//		str = '<img src="../img/jiaoyan2.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == -1)
//		str = '<img src="../img/jiaoyan3.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
	
	var htmlStr = ''+		
				'<tr>'+
				'	<td>'+ data.shipidnumber +'</td>'+
				'	<td>'+ data.shipname +'</td>'+
//				'	<td>'+ data.technicalparameter +'</td>'+
				'</tr>';
	
	$('#TableShip').append(htmlStr);
}

function bindShipEditList(data){
//	var str;
//	if(data.verifyresult >0)
//		str = '<img src="../img/jiaoyan1.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == 0)
//		str = '<img src="../img/jiaoyan2.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
//	else if(data.verifyresult == -1)
//		str = '<img src="../img/jiaoyan3.png" alt="" style="width:20px;height:20px;" data-toggle="modal" data-target="#modalCheck" onclick="getVerify(\''+data.shipidnumber+'\')" title="点击查看详情">'; 
	
	var htmlStr = ''+		
				'<tr id="tr_'+ data.shipidnumber +'">'+
				'	<td>'+ data.shipidnumber +'</td>'+
				'	<td>'+ data.shipname +'</td>'+
//				'	<td><input type="text" class="form-control" name="technicalparameter" id="'+data.shipidnumber+'" value=\"'+ data.technicalparameter +'"/></td>'+
				'	<td>'+
				'		<input type="hidden" name="ship" id="ship'+data.shipidnumber+'" value=\''+ JSON.stringify(data) +'\'>'+
				'		<button onclick="deleteShip(\''+data.shipidnumber+'\');" type="button" class="btn btn-danger btn-sm" style="margin-right: 10px;">'+
				'			<span class="glyphicon glyphicon-minus"></span>'+
				'		</button>'+
				'	</td>'+
				'</tr>';

	$('#TableShip').append(htmlStr);
}


emptyTr();
function emptyTr(){
	if($('#' + coordinateID).find('tbody').length == 0){
		var htmlStr = ''+		
					'  <tbody class="emptyTr">'+
					'		<tr>	'+
					'			<td colspan="6" style="font-size:14px; color:#810202;">	'+
					'				单击“添加坐标”增加坐标的行信息！	'+
					'			</td>	'+
					'		</tr>	'+
					'  </tbody>';
		
		$('#' + coordinateID).append(htmlStr);
	}
}